package common;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.Properties;

public class CommonDemo {

    /**
     * 获取property配置文件的值
     */
    public String getPropertyValue(String fileName, String key) throws Exception{
        Properties properties = new Properties();
        properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));
        String result = properties.getProperty(key);
        return result;
    }

    /**
     * 获取chrome浏览器驱动对象
     * @return
     */
    public ChromeDriver getChromeDriver() throws Exception{
        String chromeDriverPath = getPropertyValue("config.properties", "webdriver.chrome.driver");
        System.setProperty("webdriver.chrome.driver", chromeDriverPath);
        ChromeDriver chromeDriver = new ChromeDriver();
        return chromeDriver;
    }
    //获取文件路径
    public String getUserDir(String url){
       String filepath = "file:///" + (System.getProperty("user.dir")+"/"+url).replace("\\", "/");
       return filepath;
    }

    /**
     *
     * @param driver
     * @param seconds 秒
     * @return
     */
    public WebDriverWait getWebDriverWait(WebDriver driver, long seconds){
        WebDriverWait driverWait = new WebDriverWait(driver, Duration.ofSeconds(seconds));
        return driverWait;
    }

    /**
     * 获取chrome浏览器驱动对象
     * @return
     */
    public ChromeDriver getChromeDriverWithHeadlessMode() throws Exception{
        String chromeDriverPath = getPropertyValue("config.properties", "webdriver.chrome.driver");
        System.setProperty("webdriver.chrome.driver", chromeDriverPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("headless");
        ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
        return chromeDriver;
    }
}
